package cn.itcast.bos.utils;

import java.lang.reflect.ParameterizedType;


import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	// 模型驱动
	protected T model;

	@Override
	public T getModel() {
		return model;
	}

	// 创建 构造器
	public BaseAction() {
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> modelClass = (Class<T>) parameterizedType
				.getActualTypeArguments()[0];
			try {
				model = modelClass.newInstance();
			} catch (InstantiationException | IllegalAccessException e) {
				System.out.println("构造器创建失败");
				e.printStackTrace();
			}
	}
	
	//分页查询优化
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	protected void pushPageDataToValueStack(Page<T> pageDate){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("total", pageDate.getTotalElements());
		map.put("rows", pageDate.getContent());
		// 值栈
		ActionContext.getContext().getValueStack().push(map);
	}  
	
	
}
